﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class ThemSachDAO
    {
        private static string ChuoiKetNoi = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=DB/DATA.mdb";

        protected static OleDbConnection MoKetNoi()
        {
            OleDbConnection KetNoi = new OleDbConnection(ChuoiKetNoi);
            KetNoi.Open();
            return KetNoi;
        }

        public static bool ThemSach(ThemSachDTO s)
        {
            OleDbParameter ThamSo;
            OleDbConnection KetNoi = null;

            KetNoi = MoKetNoi();

            string CauTruyVan = "INSERT INTO Sach VALUES(@MaSach, @TuaSach, @TheLoai, @TacGia, @NamXB, @NXB, @NgayNhap, @TriGia, @SoLuong, @NguoiTiepNhan)";

            OleDbCommand Lenh = new OleDbCommand(CauTruyVan, KetNoi);

            ThamSo = new OleDbParameter("@MaSach", OleDbType.VarChar);
            ThamSo.Value = s.Masach;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@TuaSach", OleDbType.VarChar);
            ThamSo.Value = s.Tensach;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@TheLoai", OleDbType.VarChar);
            ThamSo.Value = s.Theloai;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@TacGia", OleDbType.VarChar);
            ThamSo.Value = s.Tacgia;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@NamXB", OleDbType.VarChar);
            ThamSo.Value = s.NamXB;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@NXB", OleDbType.VarChar);
            ThamSo.Value = s.NXB;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@NgayNhap", OleDbType.DBDate);
            ThamSo.Value = Convert.ToDateTime(s.Ngaynhap);
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@TriGia", OleDbType.Integer);
            ThamSo.Value = s.Trigia;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@SoLuong", OleDbType.Integer);
            ThamSo.Value = s.SL;
            Lenh.Parameters.Add(ThamSo);

            ThamSo = new OleDbParameter("@NguoiTiepNhan", OleDbType.VarChar);
            ThamSo.Value = s.Nguoinhap;
            Lenh.Parameters.Add(ThamSo);

            if (Lenh.ExecuteNonQuery() != 0)
                return true;
            return true;
        }

        public static bool KiemTra(ThemSachDTO s)
        {
            OleDbParameter ThamSo;
            OleDbConnection KetNoi = null;
            try
            {
                KetNoi = MoKetNoi();

                string CauTruyVan = "SELECT MaSach FROM Sach WHERE MaSach = @MaSach";

                OleDbCommand Lenh = new OleDbCommand(CauTruyVan, KetNoi);

                ThamSo = new OleDbParameter("@MaSach", OleDbType.VarChar);
                ThamSo.Value = s.Masach;
                Lenh.Parameters.Add(ThamSo);

                OleDbDataReader data = Lenh.ExecuteReader();
                if (data.Read())
                {
                    MessageBox.Show("Mã sách đã tồn tại !\nVui lòng nhập lại ...");
                    return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kết nối thất bại !");
                return false;
            }
            finally
            {
                if (KetNoi != null && KetNoi.State == System.Data.ConnectionState.Open)
                    KetNoi.Close();
            }
            return true;
        }
    }
}
